package com.bootdo.wechat.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.redis.shiro.RedisManager;
import com.bootdo.wechat.domain.AccessVO;
import com.bootdo.wechat.utils.UUIDUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wechat.domain.MpConfigDO;
import com.bootdo.wechat.service.MpConfigService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import redis.clients.jedis.Jedis;

/**
 * 
 * 
 * @author songjw
 * @email songjw1993@126.com
 * @date 2018-07-27 16:10:16
 */
 
@Controller
@RequestMapping("/wechat/mpConfig")
public class MpConfigController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private MpConfigService mpConfigService;
	
	@GetMapping()
	@RequiresPermissions("wechat:mpConfig:mpConfig")
	String MpConfig(){
	    return "wechat/mpConfig/mpConfig";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wechat:mpConfig:mpConfig")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MpConfigDO> mpConfigList = mpConfigService.list(query);
		int total = mpConfigService.count(query);
		PageUtils pageUtils = new PageUtils(mpConfigList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wechat:mpConfig:add")
	String add(){
	    return "wechat/mpConfig/add";
	}

	@GetMapping("/edit/{mpId}")
	@RequiresPermissions("wechat:mpConfig:edit")
	String edit(@PathVariable("mpId") String mpId,Model model){
		MpConfigDO mpConfig = mpConfigService.get(mpId);
		model.addAttribute("mpConfig", mpConfig);
	    return "wechat/mpConfig/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wechat:mpConfig:add")
	public R save( MpConfigDO mpConfig){
		if(mpConfigService.save(mpConfig)>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 保存并接入
	 * @param mpConfig
	 * @return
	 */
	@ResponseBody
	@PostMapping("/access")
	public R access( MpConfigDO mpConfig){
		String id = UUIDUtil.get12UUID();
		mpConfig.setId(id);
		if(mpConfigService.save(mpConfig)>0){
			R r = new R();
			r.put("id",id);
			return r;
		}
		return R.error();
	}

	/**
	 * 接入需要在微信平台配置的信息
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/doAccess/{id}")
	String doAccess(@PathVariable("id") String id,Model model){
		logger.info("---=================");
		MpConfigDO mpConfig = mpConfigService.selectById(id);
		RedisManager redisManager= new RedisManager();
		Jedis jedis = redisManager.getJedis();
		String url =jedis.get("HostUrl")+"/mp/portal?id="+id;
		AccessVO accessVO = new AccessVO();
		accessVO.setUrl(url);
		accessVO.setToken(mpConfig.getMpToken());
		accessVO.setEncodingAESKey(mpConfig.getMpAesKey());

		model.addAttribute("accessVo", accessVO);
		return "wechat/mpConfig/access";
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wechat:mpConfig:edit")
	public R update( MpConfigDO mpConfig){
		mpConfigService.update(mpConfig);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wechat:mpConfig:remove")
	public R remove( String mpId){
		if(mpConfigService.remove(mpId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wechat:mpConfig:batchRemove")
	public R remove(@RequestParam("ids[]") String[] mpIds){
		mpConfigService.batchRemove(mpIds);
		return R.ok();
	}
	
}
